const express = require('express');
const router = express.Router();
const db = require('../config/db');

// 获取菜品分类
router.get('/categories', async (req, res) => {
  try {
    const [categories] = await db.query('SELECT * FROM categories ORDER BY sort_order');
    res.json({ code: 0, data: categories });
  } catch (error) {
    res.status(500).json({ message: '获取分类失败' });
  }
});

// 获取菜品列表
router.get('/dishes', async (req, res) => {
  try {
    const { category_id } = req.query;
    let sql = 'SELECT * FROM dishes WHERE status = 1';
    if (category_id) {
      sql += ' AND category_id = ?';
    }
    const [dishes] = await db.query(sql, [category_id]);
    res.json({ code: 0, data: dishes });
  } catch (error) {
    res.status(500).json({ message: '获取菜品失败' });
  }
});

module.exports = router;